#!/bin/bash -e

function info() {
echo Usage: `basename $0` '[-i(indel) -l in.bed -m(somatic) -g(single_cell)] <snp.vcf|indel.vcf>'
exit 65
}

while getopts ":p:il:mgh" opts
do
case $opts in
	i) indel=true;; 
	m) somatic=T;;
	g) single_cell=T;;
	l) interval=$OPTARG;;
	p) out_prefix=$OPTARG;;
	\?) info;;
esac
done
shift $(($OPTIND - 1))

test $# -lt 1 && info


. /mnt/ilustre/app/medical/tools/.var



IFS=$'\n'

if test -z "$indel"; then

	for dat in $(less $tools_path/script/filters_snp.txt); do

	filter=$(echo $dat |cut -f2)
	filter_str=$(echo $dat|cut -f1)
	filter0=$(echo --filterExpression \"$filter\" --filterName "\"${filter_str}\" ")
	filters_snp=$filters_snp$filter0
	done
	filters_snp=$(echo $filters_snp |perl -pe 's/ $//')



	snp_sh=`mktemp snp.sh.XXXXX`
	echo;echo;echo gatk VariantFiltration
	echo java $j_mem -jar $gatk -R $ref_genome -T VariantFiltration -o $out_prefix.snp.filtered.vcf -L $interval --variant $1 $filters_snp |tee $snp_sh

	. $snp_sh

else

	for dat in $(less $tools_path/script/filters_indel.txt); do 

	filter=$(echo $dat |cut -f2)
	filter_str=$(echo $dat|cut -f1)
	filter0=$(echo --filterExpression \"$filter\" --filterName "\"${filter_str}\" ")
	filters_indel=$filters_indel$filter0
	done
	filters_indel=$(echo $filters_indel |perl -pe 's/ $//')
		
	indel_sh=`mktemp snp.sh.XXXXX`
	echo;echo;echo gatk VariantFiltration
	echo java $j_mem -jar $gatk -R $ref_genome -T VariantFiltration -o $out_prefix.indel.filtered.vcf -L $interval --variant $1 $filters_indel |tee $indel_sh

	. $indel_sh
fi

. $cmd_done